package com.erplan.swt.app.gx;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import com.erplan.swt.app.gx.db.DBConnection;
import com.erplan.swt.app.gx.dialogs.GxAbout;

public class GxMenuBar {

	public static void createMenuBar() {
//		if(Gx.shell.getMenu() != null){
//			(Gx.shell.getMenu()).dispose();
//		}
//		if(P5.menuBar != null){
//			P5.menuBar.dispose();
//		}
//

		//Creating Menu bar
		Gx.menuBar = new Menu(Gx.shell, SWT.BAR);
		Gx.shell.setMenuBar(Gx.menuBar);

		//Creating File Menu
		MenuItem mainMenuItem = new MenuItem(Gx.menuBar, SWT.CASCADE);
		mainMenuItem.setText("File");
		mainMenuItem.addListener(SWT.Arm, GxStatusBar.armListener);
		Menu mainMenu = new Menu(Gx.shell, SWT.DROP_DOWN);
		mainMenu.addListener(SWT.Hide, GxStatusBar.hideListener);
		mainMenu.addListener(SWT.Show, GxStatusBar.showListener);
		mainMenuItem.setMenu(mainMenu);

		MenuItem menuItem = new MenuItem(mainMenu, SWT.PUSH);
		menuItem.setText("Option &A\tAlt+A");
		menuItem.setAccelerator(SWT.ALT + 'A');
		menuItem.addListener(SWT.Arm, GxStatusBar.armListener);
		menuItem.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event e) {

			}
		});


		menuItem = new MenuItem(mainMenu, SWT.SEPARATOR);

		menuItem = new MenuItem(mainMenu, SWT.PUSH);
		menuItem.setText("Option &B\tAlt+B");
		menuItem.setAccelerator(SWT.ALT + 'B');
		menuItem.addListener(SWT.Arm, GxStatusBar.armListener);
		menuItem.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event e) {

			}
		});

		menuItem = new MenuItem(mainMenu, SWT.SEPARATOR);

		menuItem = new MenuItem(mainMenu, SWT.PUSH);
		menuItem.setText("E&xit\tAlt+X");
		menuItem.setAccelerator (SWT.ALT + 'X');
		menuItem.addListener(SWT.Arm, GxStatusBar.armListener);
		menuItem.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event e) {
				MessageBox messageBox = new MessageBox (Gx.shell, SWT.APPLICATION_MODAL | SWT.ICON_QUESTION | SWT.YES | SWT.NO);
				messageBox.setText(Gx.toolTextSmall);
				messageBox.setMessage("Close " + Gx.toolTextSmall + " ?");
				if(messageBox.open() == SWT.YES){
					Gx.display.dispose();
					DBConnection.close();
					System.exit(0);
				}
			}
		});


		//Creating Help Menu
		MenuItem helpMenuItem = new MenuItem(Gx.menuBar, SWT.CASCADE);
		helpMenuItem.setText("Help");
		helpMenuItem.addListener(SWT.Arm, GxStatusBar.armListener);
		Menu helpMenu = new Menu(Gx.shell, SWT.DROP_DOWN);
		helpMenu.addListener(SWT.Hide, GxStatusBar.hideListener);
		helpMenu.addListener(SWT.Show, GxStatusBar.showListener);
		helpMenuItem.setMenu(helpMenu);

		menuItem = new MenuItem(helpMenu, SWT.CASCADE);
		menuItem.setText("About " + Gx.toolTextSmall);
		menuItem.addListener(SWT.Arm, GxStatusBar.armListener);
		menuItem.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event e) {
				GxAbout p5About = new GxAbout();
				Shell aboutDialog = p5About.createAboutDialog(Gx.shell, Gx.display);
				aboutDialog.open();

				while (!aboutDialog.isDisposed ()) {
					if (!Gx.display.readAndDispatch ()) Gx.display.sleep ();
				}
			}
		});
	}

}
